package com.nicolasprof.profgallery.utils {
	import com.hexagonstar.util.debug.Debug;
	import com.nicolasprof.profgallery.ui.ToolTip;

	import flash.display.DisplayObject;
	import flash.display.Stage;
	import flash.events.IEventDispatcher;
	import flash.events.MouseEvent;
	import flash.utils.Dictionary;

	/**
	 * @author Nicolas Prof
	 */
	public class TooltipHandler extends Object {
		
		private static var _instance:TooltipHandler = new TooltipHandler();
		private var _list : Dictionary;
		private var _tooltip : ToolTip;
		private var _stage : Stage;
		private var _isEnabled : Boolean;

		
		
		public function TooltipHandler() {
			if( _instance ) throw new Error( "Singleton and can only be accessed through Singleton.getInstance()" );
			_list = new Dictionary(true);
		}	
		public function add($object : DisplayObject, $text : String) : void {
			if($object.parent != null){
				_stage = $object.stage;
				_list[$object] = $text;
				addListeners($object);
			}
		}	
		private function addListeners($dispatcher : IEventDispatcher) : void {
			$dispatcher.addEventListener(MouseEvent.MOUSE_OVER, onOver, false, 0, true);
			$dispatcher.addEventListener(MouseEvent.MOUSE_OUT, onOut, false, 0, true);
			$dispatcher.addEventListener(MouseEvent.MOUSE_MOVE, onMove, false, 0, true);
		}
		public static function getInstance():TooltipHandler {
			return _instance;
		}
		public function moveTo($x : int, $y : int) : void {
			if(_tooltip == null) return;
			_tooltip.x = $x;
			_tooltip.y = $y;
		}
		public function remove($object : DisplayObject) : void {
			removeListeners($object);
			delete _list[$object];
		}
		private function removeListeners($dispatcher : IEventDispatcher) : void {
			$dispatcher.removeEventListener(MouseEvent.MOUSE_OVER, onOver);
			$dispatcher.removeEventListener(MouseEvent.MOUSE_OUT, onOut);
			$dispatcher.removeEventListener(MouseEvent.MOUSE_MOVE, onMove);
		}
		public function removeTooltip() : void {
			if(_tooltip != null) {
				_stage.removeChild(_tooltip);
				_tooltip = null;
			}
		}
		public function showTooltip($text : String, $x : Number, $y : Number) : void {
			_tooltip = new ToolTip($text);
			_stage.addChild(_tooltip);
			moveTo($x, $y);
		}
		/**
		 * Events
		 */
		private function onMove(e : MouseEvent) : void {
			moveTo(_stage.mouseX, _stage.mouseY);
		}
		private function onOver(e : MouseEvent) : void {
			removeTooltip();
			showTooltip(_list[e.currentTarget], _stage.mouseX, _stage.mouseY);
		}
		private function onOut(e : MouseEvent) : void {
			removeTooltip();
		}
		

		/**
		 * Getters / Setters
		 */
		public function get enabled() : Boolean {
			return _isEnabled;
		}
		public function set enabled($state : Boolean) : void {
			_isEnabled = $state;
		}
		public function get stage() : Stage {
			return _stage;
		}
		public function set stage($stage : Stage) : void {
			Debug.trace("Stage registered");
			_stage = $stage;
		}
	}
}
